import { formatTime } from "../../utils/util";

// pages/studentAdd/studentAdd.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId:'' as any,
    studentId:'',
    studentName:'',
    classifyShow: false,
    subjectShow: false,
    formShow:false,
    surplus:'',
    price:'',
    times:'',
    unitPrice:'',
    currentPrice:'',
    reamrk:'',
    subjects: [
      {
        name: '语文',
      },
      {
        name: '数学',
      },
      {
        name: '英语',
      },
      {
        name: '物理',
      },
      {
        name: '化学',
      },
      {
        name: '生物',
      },
      {
        name: '阅读与写作',
      },
      {
        name: '科学实验室',
      },
      {
        name: '英语新概念',
      },
    ],
    classifys:[{
      name:'小学'
    },{
      name:'初中'
    },{
      name:'高中'
    }],
    forms:[{
      name:'一对一'
    },{
      name:'一对二'
    },{
      name:'小班'
    }]
  },
  subjectClose() {
    this.setData({ subjectShow: false });
  },
  classifyClose() {
    this.setData({ classifyShow: false });
  },

  formClose() {
    this.setData({  formShow: false });
  },


  subjectSelect(event) {
    console.log(event.detail);
    this.setData({
      subject:event.detail.name
    })
  },

  classifySelect(event){
    this.setData({
      classify:event.detail.name
    })
  },

  formSelect(event){
    this.setData({
      form:event.detail.name
    })
  },

  subjectOpen(){
    this.setData({ subjectShow: true });
  },

  classifyOpen(){
    this.setData({  classifyShow: true });
  },

  formOpen(){
    this.setData({  formShow: true });
  },
  async submit(event){
   
    let data = event.detail.value
    console.log(data);
    if(data.name==''||data.subject==''||data.classify==''||data.form==''||data.price==''||data.times==''||data.unitPrice==''){
      wx.showToast({
        title:'填写不完整',
        icon:'error'
      })
      return
    }
    wx.cloud.database().collection('signs').add({
      data:data
    }).then((res)=>{
      wx.showToast({
        title:'添加签到记录成功',
      });
      this.setData({
        name:'',
        subject:'',
        classify:'',
        reamrk:'',
        form:'',
        price:'',
        unitPrice:'',
        times:'',
        surplus:'',
        currentPrice:'',
        studentId:'',
        studentName:''
      })
    })
  },

  getUnitPrice(event:WechatMiniprogram.CustomEvent){
    let times = this.data.times;
    let price = this.data.price;
    let unitPrice = (Number(price)/Number(times)).toFixed(2);
    this.setData({
      unitPrice:unitPrice
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(event) {
    console.log(event);
    this.setData({
      signId:event.signId,
    });
    wx.cloud.database().collection('signs').doc(event.signId).get().then(res=>{
     
      console.log(res);
      console.log( formatTime(new Date(res.data._createTime)))
      
      this.setData({
        signId:res.data._id,
        studentName:res.data.studentName,
        studentId:res.data.studentId,
        subject:res.data.subject,
        classify:res.data.classify,
        reamrk:res.data.reamrk,
        form:res.data.form,
        price:res.data.price,
        unitPrice:res.data.unitPrice,
        times:res.data.times,
        surplus:res.data.surplus,
        currentPrice:res.data.currentPrice,
        _createTime:formatTime(new Date(res.data._createTime)),
        userName:res.data.userName,
        userPhone:res.data.userPhone,
        userRole:res.data.userRole
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})